package com.situ.mall.common.mapper;

import com.situ.mall.common.entity.Category;
import com.situ.mall.common.entity.Product;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.situ.mall.common.vo.CartVO;
import com.situ.mall.common.vo.EditProductVO;
import com.situ.mall.common.vo.IndexProductVO;
import com.situ.mall.common.vo.ProductVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author JavaWizard
 * @since 2023-09-12
 */
@Mapper
public interface ProductMapper extends BaseMapper<Product> {
/* 后台请求 */
    //查询用户表格
    List<ProductVO> list(@Param("categoryId")Integer categoryId, @Param("name")String name, @Param("edition")String edition);

    //新增保存
    @Insert("insert into product(name,category_id,edition,color,model,price,status,img_path,title) values (#{name},#{categoryId},#{edition},#{color},#{model},#{price},#{status},#{imgPath},#{title})")
    void save(Product product);

    //更新
    //避免product中有不传值的数据被更新成null，该语句写成动态sql
    void update(Product product);

    //软删除
    @Update("update product set status = 1 where id = #{id}")
    void delete(Integer id);

    //根据商品名查
    Product selectByName(@Param("name")String name, @Param("id")Integer id);

    //根据商品id查
    @Select("select * from product where id = #{id}")
    Product selectById(Integer id);

    //根据二级分类id查
    @Select("select * from product where category_id = #{id} and status != 1")
    List<Product> selectByCategoryId(Integer id);

    //查询二级分类
    @Select("select * from category where status != 1 and parent_id != 0")
    List<Category> all();

    //编辑——根据ID查询该行数据带到前台
    @Select("select product.*,category.parent_id 'topCategoryId' from product inner join category on product.category_id = category.id where product.id = #{id}")
    EditProductVO edit(Integer id);


/* 前端请求 */
    //传一个分类id，查该分类下最便宜的单品
    @Select("select * from product where category_id = #{id} and status = 0 order by price limit 0,1")
    IndexProductVO selectCheapest(Integer id);

    //根据商品名查，返回同名的所有商品
    @Select("select * from product where name = #{name} and status = 0 order by price asc")
    List<ProductVO> getByName(String name);

    //查商品
    List<CartVO> selectByIds(@Param("ids")String[] split, @Param("memberId")Integer memberId);


}
